package com.hisense.ovcloud.common3.validator;

import lombok.AllArgsConstructor;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import java.util.List;

@AllArgsConstructor
public class BaseValidator implements Validator {
    private Class reqClazz;
    private List<String> parametersToCheck;

    @Override
    @SuppressWarnings("unchecked")
    public boolean supports(Class<?> clazz) {
        return reqClazz.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        parametersToCheck.forEach(parameter -> {
            ValidationUtils.rejectIfEmptyOrWhitespace(
                    errors, parameter, "field.required", String.format("Parameter %s should not be null!", parameter));
        });
    }
}
